package com.easysale.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.easysale.domain.Product;

@Repository
public class ProductDAOImpl implements ProductDAO {

	@Autowired
	SessionFactory sessionFactory;
	
	@Override
	public void addProduct(Product product) {
		sessionFactory.getCurrentSession().save(product);
	}

	@Override
	public List<Product> products() {
		return sessionFactory.getCurrentSession().createQuery("from Product").list();
	}

	@Override
	public void deleteProduct(Integer productId) {
		Product product = (Product) sessionFactory.getCurrentSession().load(
				Product.class, productId);
        if (null != product) {
            sessionFactory.getCurrentSession().delete(product);
        }
	}

}
